// pages/orders/evaluate.js
const ins = getApp()

Page({
  data: {
    form: {
      id: null,
      pid: null,
      rate: 0,
      comment: ''
    }
  },
  onLoad: function (options) {
    this.setData({
      'form.id': options.id,
      'form.pid': options.pid
    })
  },
  valueChange(e) {
    this.setData({
      ['form.' + e.currentTarget.dataset.field]: e.currentTarget.dataset.field == "rate" ? Number.parseInt(e.detail.value) : e.detail.value
    })
  },
  submit() {
    wx.showLoading({title: '请稍候'})

    ins.api.requestApi('request/evaluate', 'POST', this.data.form, {
      complete: () => {
        wx.hideLoading()
      },
      success: (resp) => {
        if(resp.data.ret == ins.globalData.request_ret.SUCCESS) {
          wx.navigateBack({
            delta: 1,
            complete: () => {
              wx.showToast({
                title: '评价成功',
                icon: 'success'
              })
            }
          })
        } else {
          wx.showToast({
            title: resp.data.data != null ? resp.data.data[0] : resp.data.msg,
            icon: 'error'
          })
        }
      },
      fail: () => {
        wx.showToast({
          title: '服务器错误',
          icon: 'error'
        })
      }
    })
  }
})